<!DOCTYPE html>
<html class="x-admin-sm">
{% load staticfiles %}
<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="{% static 'css/font.css' %}">
    <link rel="stylesheet" href="{% static 'css/xadmin.css' %}">
    <link rel="stylesheet" href="{% static 'css/theme1212.min.css' %}">
    <script type="text/javascript" src="{% static 'js/jquery.min.js' %}" charset="utf-8"></script>
    <script type="text/javascript" src="{% static 'lib/layui/layui.js' %}" charset="utf-8"></script>
    <script type="text/javascript" src="{% static 'lib/jquery.form.js' %}" charset="utf-8"></script>
    <script type="text/javascript" src="{% static 'lib/jquery.cookie.js' %}" charset="utf-8"></script>
    <script type="text/javascript" src="{% static 'js/xadmin.js' %}"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
            <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
            <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]--></head>

<body>
<div class="layui-fluid">
    <div class="layui-row layui-card" style="padding: 50px;">
        <form class="layui-form" id="KanjiaInfo">
            {% csrf_token %}
            <div class="layui-form-item">
                <label for="title" class="layui-form-label">
                    <span class="x-red">*</span>店铺名称</label>
                <div class="layui-input-inline col-md-6">
                    <input id="title" name="title" lay-verify="required" class="layui-input"></div>
            </div>
            <div class="layui-form-item">
                <label for="title" class="layui-form-label">
                    <span class="x-red">*</span>店铺LOGO</label>
                <div class="layui-input-inline">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="logo_btn">上传图片</button>
                        <div class="layui-upload-list">
                            <img class="logo-img" id="logo_preview" style="width: 80px;height: 80px;">
                            <input type="hidden" name="logo" id="logo">
                            <p id="demoText"></p>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="title" class="layui-form-label">
                    <span class="x-red">*</span>店铺展示
                </label>

                <div class="layui-input-inline">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="images_btn">上传图片</button>
                        <div class="layui-upload-list">
                            <div class="layui-upload-list" id="images_preview" style="display:inline-flex;"></div>
                            <input type="hidden" name="images" id="images">
                            <p id="demoText2" class="x-red">最多上传6张，建议尺寸750*450px</p>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="sub_time" class="layui-form-label">
                    <span class="x-red">*</span>联系电话</label>
                <div class="layui-input-inline">
                    <input type="text" id="phone" name="phone" lay-verify="required|phone"
                           autocomplete="off" class="layui-input" placeholder="请填写电话号码"></div>
            </div>
            <div class="layui-form-item x-city" id="x-city">
                <label class="layui-form-label">联系地址</label>
                <div class="layui-input-inline">
                    <select name="province_address" lay-filter="province">
                        <option value="">请选择省</option>
                    </select>
                </div>
                <div class="layui-input-inline">
                    <select name="city_address" lay-filter="city">
                        <option value="">请选择市</option>
                    </select>
                </div>
                <div class="layui-input-inline">
                    <select name="country_address" lay-filter="area">
                        <option value="">请选择县/区</option>
                    </select>
                </div>
                <div class="layui-input-inline"><input type="text" id="address" name="address" lay-verify=""
                                                       autocomplete="off" class="layui-input" placeholder="请填写详细地址">
                </div>
            </div>
            <div class="layui-row">
                <label for="desc" class="layui-form-label">
                    <span class="x-red">*</span>店铺简介</label>
                <div class="layui-input-inline ">
                    <textarea id="desc" name="desc" style="display: none;"></textarea></div>
            </div>
            <div class="layui-form-item">
                <label for="L_repass" class="layui-form-label"></label>
                <button class="layui-btn" lay-filter="add" lay-submit="">保存</button>
            </div>
        </form>
    </div>
</div>
<script type="text/javascript" src="{% static 'js/xcity.js' %}"></script>
<script>
    layui.use(['form', 'layer', 'laydate', 'upload', 'layedit', 'jquery'],
        function () {
            $ = layui.jquery;
            form = layui.form,
                layer = layui.layer,
                laydate = layui.laydate,
                upload = layui.upload,
                layedit = layui.layedit;
            ;
            upload.config.data = {
                'csrfmiddlewaretoken': $.cookie("csrftoken")
            }
            upload.config.url = '{% url "common:upload" %}';
            //执行一个laydate实例
            laydate.render({
                elem: '#start_time' //指定元素
                , type: 'datetime'
            });
            //初始化城市联动
            $('#x-city').xcity();
            //执行一个laydate实例
            laydate.render({
                elem: '#end_time' //指定元素
                , type: 'datetime'
            });
            //自定义验证规则
            form.verify({
                nikename: function (value) {
                    if (value.length < 5) {
                        return '昵称至少得5个字符啊';
                    }
                },
                pass: [/(.+){6,12}$/, '密码必须6到12位'],
                repass: function (value) {
                    if ($('#L_pass').val() != $('#L_repass').val()) {
                        return '两次密码不一致';
                    }
                }
            });
            //logo上传
            var uploadInst = upload.render({
                elem: '#logo_btn'
                , size: 2048
                , exts: 'png|jpg|gif'
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#logo_preview').attr('src', result); //图片链接（base64）
                    });
                }
                , done: function (res) {
                    //如果上传失败
                    console.log(res)
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    //上传成功
                    $('#logo').val(res.name)
                }
                , error: function () {
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function () {
                        uploadInst.upload();
                    });
                }
            });
            //店内景上传
            var uploadInst2 = upload.render({
                elem: '#images_btn'
                , size: 2048
                , multiple: true
                , exts: 'png|jpg|gif'
                , before: function (obj) {
                    if ($('#images_preview').children().length >= 6) {
                        return layer.msg('最多上传6张图片');
                    }
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#images_preview').append('<img src="' + result + '" alt="' + file.name + '" class="layui-upload-img" style="width: 250px;height: 150px;padding:5px;">')
                    });
                }
                , done: function (res) {
                    //如果上传失败
                    console.log(res)
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    //上传成功
                    let images = $('#images').val()
                    if (images) {
                        images += ',' + res.name
                    } else {
                        images += res.name
                    }
                    $('#images').val(images)
                }
                , error: function () {
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText2');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function () {
                        uploadInst2.upload();
                    });
                }
            });
            //富文本编辑器
            layedit.set({
                uploadImage: {
                    url: '{% url "common:upload" %}' //接口url
                    , type: 'post' //默认post
                    , tdata: {
                        'csrfmiddlewaretoken': $.cookie("csrftoken")
                    }
                }
            });
            var desc_t = layedit.build('desc', {
                height: 180,//设置编辑器高度,
                tool: [
                    'strong' //加粗
                    , 'italic' //斜体
                    , 'underline' //下划线
                    , 'del' //删除线

                    , '|' //分割线

                    , 'left' //左对齐
                    , 'center' //居中对齐
                    , 'right' //右对齐
                    , 'link' //超链接
                    , 'unlink' //清除链接
                    , 'face' //表情
                    , 'image' //插入图片
                    , 'help' //帮助
                ]
            });

            //监听提交
            form.on('submit(add)',
                function (data) {
                    $('#desc').val(layedit.getContent(desc_t));
                    debugger;
                    $.post("{% url 'shop:shop_add' %}", $(data.form).serialize(), function (res) {
                            debugger;
                            console.log(res);
                            if (res['status'] == 0) {

                                layer.alert("增加成功,请关闭tab刷新页面！", {
                                        icon: 6
                                    },
                                    function () {
                                        debugger;
                                    });
                            } else {
                                alert(res['detail'])
                            }
                        }
                    );
                    return false;
                });
        });
</script>
</body>
</html>